package com.e.takeaway.framgent;

import android.content.Intent;
import android.graphics.Outline;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewOutlineProvider;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import com.e.takeaway.DetailActivity;
import com.e.takeaway.R;
import com.e.takeaway.SearchActivity;
import com.e.takeaway.adapter.MerChantAdapter;
import com.e.takeaway.adapter.SearchAdapter;
import com.e.takeaway.po.MerChant;
import com.e.takeaway.util.MyRetrofit;
import com.e.takeaway.service.RequestService;
import com.e.takeaway.util.MyImageLoader;
import com.google.android.material.tabs.TabLayout;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

/**
 * A simple {@link Fragment} subclass.
 * create an instance of this fragment.
 */
public class MainFramgent extends Fragment {
    private Banner banner;
    private Button btn_search;
    private MyImageLoader myImageLoader;
    private MerChantAdapter merChantAdapter;
    private List<MerChant> merChants;
    private ArrayList<Integer> photos;
    private ListView listView;
    private TabLayout tab_title;
    private EditText ed_input;
    private String mTitles[] = {"面条米粉","烧烤龙虾","甜点饮品", "甜蜜蛋糕","生鲜果蔬","超市便利"};

    public static MainFramgent newInstance(String para1){
        MainFramgent mainFramgent = new MainFramgent();
        Bundle b = new Bundle();
        b.putString("par1",para1);
        mainFramgent.setArguments(b);
        return mainFramgent;
    }



    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v = inflater.inflate(R.layout.fragment_main, container, false);
        initData();
        banner = (Banner) v.findViewById(R.id.banner);
        tab_title = (TabLayout)v.findViewById(R.id.tab_title);
        listView = (ListView) v.findViewById(R.id.list_shop_main);
        ed_input = (EditText)v.findViewById(R.id.ed_input);
        btn_search = (Button)v.findViewById(R.id.btn_search);
        btn_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String keyWord = ed_input.getText().toString();
                if (keyWord.equals(""))
                    Toast.makeText(getActivity(),"搜索框不能为空", Toast.LENGTH_SHORT).show();
                else {
                    Retrofit retrofit = MyRetrofit.getRetrofit();
                    RequestService request = retrofit.create(RequestService.class);
                    Call<List<MerChant>> call = request.getSearchMerchant(keyWord);
                    call.enqueue(new Callback<List<MerChant>>() {
                        @Override
                        public void onResponse(Call<List<MerChant>> call, Response<List<MerChant>> response) {

                            Intent intent = new Intent(getActivity(), SearchActivity.class);
                            Bundle bundle = new Bundle();
                            ArrayList list = new ArrayList();
                            list.add(response.body());
                            bundle.putParcelableArrayList("listMerchant",list);
                            intent.putExtras(bundle);
                            startActivity(intent);
                        }

                        @Override
                        public void onFailure(Call<List<MerChant>> call, Throwable t) {

                        }
                    });


                }
            }
        });
        requestData(0);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                MerChant merChant = merChants.get(i);
                Intent intent = new Intent(getActivity(), DetailActivity.class);
                Bundle b = new Bundle();
                b.putSerializable("mer",merChant);
                intent.putExtras(b);
                startActivity(intent);
            }
        });
        initView();
        return v;
    }

    private void requestData(int num) {
        Retrofit retrofit = MyRetrofit.getRetrofit();
        RequestService request = retrofit.create(RequestService.class);
        Call<List<MerChant>> call = request.getList(num);
        call.enqueue(new Callback<List<MerChant>>() {
            @Override
            public void onResponse(Call<List<MerChant>> call, Response<List<MerChant>> response) {
                merChants = response.body();
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                      merChantAdapter = new MerChantAdapter(getActivity(),merChants);
                      listView.setAdapter(merChantAdapter);
                    }
                });
            }

            @Override
            public void onFailure(Call<List<MerChant>> call, Throwable t) {
                System.out.println("-----------------------------------------------------error ");
            }
        });
    }


    private void initView() {
        //设置圆角
        banner.setOutlineProvider(new ViewOutlineProvider() {
            @Override
            public void getOutline(View view, Outline outline) {
                outline.setRoundRect(0, 0, view.getWidth(), view.getHeight(), 30);
            }
        });
        banner.setClipToOutline(true);
        //设置图片的样式
        banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
        //设置图片加载器
        banner.setImageLoader(myImageLoader);
        //设置轮播的动画效果
        banner.setBannerAnimation(Transformer.Default);
        //设置轮播间隔
        banner.setDelayTime(3000);
        //设置是否自动轮播
        banner.isAutoPlay(true);
        //设置指示器的位置，小圆点，居中显示
        banner.setIndicatorGravity(BannerConfig.CENTER);
        //设置图片加载地址,启动轮播
        banner.setImages(photos).start();




        //分类
        tab_title.addTab(tab_title.newTab().setText(mTitles[0]),true);
        tab_title.addTab(tab_title.newTab().setText(mTitles[1]),false);
        tab_title.addTab(tab_title.newTab().setText(mTitles[2]),false);
        tab_title.addTab(tab_title.newTab().setText(mTitles[3]),false);
        tab_title.addTab(tab_title.newTab().setText(mTitles[4]),false);
        tab_title.addTab(tab_title.newTab().setText(mTitles[5]),false);
        tab_title.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                 Toast.makeText(getActivity(),"点击了"+tab.getText(),Toast.LENGTH_SHORT).show();
                 requestData(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });




    }

    private void initData(){

        myImageLoader = new MyImageLoader();
        photos = new ArrayList<>();
        photos.add(R.drawable.lunbo1);
        photos.add(R.drawable.lunbo2);
        photos.add(R.drawable.lunbo3);
        photos.add(R.drawable.lunbo4);
    }
    public void flushData(List<MerChant> list){
        merChantAdapter = new MerChantAdapter(getActivity(),list);
        listView.setAdapter(merChantAdapter);

    }

    /**
     * 设置Ui线程刷新值
     * @param
     */
    private void showResult(List<MerChant> list1){
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
               flushData(list1);
            }
        });
    }
}